const wapper = document.querySelector(".wapper");
const textAll = document.querySelectorAll("span.text");

const texts = [
  "苹果",
  "华为",
  "三星",
  "魅族",
  "vovo",
  "oppo",
  "小米",
  "未中奖",
];
let isFlag = true;
let timer = null;

for (let i = 0; i < texts.length; i++) {
  textAll[i].innerHTML = texts[i];
}

document.querySelector(".circle").onclick = function () {
  if (!isFlag) return;
  const random = Number.parseInt(Math.random() * texts.length);
  switch (random) {
    case 0:
      run(22.5, texts[random]);
      break;
    case 1:
      run(66.5, texts[random]);
      break;
    case 2:
      run(112.5, texts[random]);
      break;
    case 3:
      run(157.5, texts[random]);
      break;
    case 4:
      run(338.5, texts[random]);
      break;
    case 5:
      run(294.5, texts[random]);
      break;
    case 6:
      run(247.5, texts[random]);
      break;
    default:
      run(201.5, texts[random]);
      break;
  }
};

/**
 *
 * @param {*} angle 每个扇形区域的中心位置的角度
 * @param {*} text
 */
function run(angle, text) {
  console.log(23);
  let begin = 0;
  let basic = 1800;
  isFlag = false;
  timer && clearInterval(timer);
  timer = setInterval(() => {
    if (begin >= angle + basic) {
      clearInterval(timer);
      timer = null;
      isFlag = true;
    }
    wapper.style.transform = `rotate(${begin}deg)`;
    begin += Math.ceil((basic + angle - begin) * 0.1);
  }, 50);
}